package com.luojialong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luojialong.constant.UserRank;
import com.luojialong.enums.AffairStatus;
import com.luojialong.exception.AffairException;
import com.luojialong.model.domain.User;
import com.luojialong.model.request.UserLoginRequest;
import com.luojialong.model.request.UserRegisterRequest;
import com.luojialong.resultVo.ResultResponse;
import com.luojialong.service.UserService;
import com.luojialong.utils.ResultUtils;
import com.luojialong.verification.UserPermissionCheck;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.luojialong.constant.UserRank.ADMIN_ROLE;
import static com.luojialong.constant.UserRank.USER_LOGIN_KEY;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;



    @PostMapping("/register")
    public ResultResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR,"用户请求参数与后端不对应");
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String userCheckPassword = userRegisterRequest.getCheckPassword();
        long res = userService.userRegister(userAccount, userPassword, userCheckPassword);
        return ResultUtils.success(res);
    }


    @PostMapping("/login")
    public ResultResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR,"用户请求参数与后端不对应");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

    @PostMapping("/logout")
    public ResultResponse<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR,"用户请求参数与后端不对应");
        }
        Integer i = userService.userLogout(request);
        return ResultUtils.success(i);
    }


    @GetMapping("/current")
    public ResultResponse<User> getCurrentUser(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_KEY);
        User currentUser = (User)userObj;
        if (currentUser == null) {
            throw new AffairException(AffairStatus.NOT_LOGIN,"用户未登录");
        }
        User safetyUser = userService.getSafetyUser(userService.getById(currentUser.getId()));
        return ResultUtils.success(safetyUser);
    }


    @GetMapping("/search")
    @UserPermissionCheck(LeastPrivilege = ADMIN_ROLE)
    public ResultResponse<List<User>> searchUsers(String username,HttpServletRequest request) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)) {
            userQueryWrapper.like("username",username);
        }
        List<User> userList = userService.list(userQueryWrapper);
        List<User> collect = userList.stream().map(user -> {
            return userService.getSafetyUser(user);
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    @PostMapping("/delete")
    @UserPermissionCheck(LeastPrivilege = ADMIN_ROLE)
    public ResultResponse<Boolean> deleteUser(@RequestBody Long id,HttpServletRequest request){
        if (id == null || id < 0) {
            throw new AffairException(AffairStatus.PARAMS_ERROR,"请求参数与后端不对应");
        }
        boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }


}
